import 'package:flutter/material.dart';
import 'package:flutterhello/api/api.dart';
import 'package:flutterhello/util/host_util.dart';
import 'package:flutterhello/widgets/my_toast.dart';
import 'package:get/get.dart';

class SignPage extends StatefulWidget {
  @override
  _SignPageState createState() => _SignPageState();
}

class _SignPageState extends State<SignPage> {
  var username = "";
  var psd1 = "";
  var psd2 = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: Column(
        children: <Widget>[
          Container(
            height: 80,
            width: 80,
            margin: EdgeInsets.only(top: 50),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Colors.blueGrey,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Text(
              'Logo',
              style: TextStyle(
                color: Colors.white,
                fontSize: 25,
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(vertical: 20),
            child: Text(
              '帐号注册',
              style: TextStyle(
                color: Colors.black54,
                fontSize: 20,
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(
              vertical: 10,
              horizontal: 30,
            ),
            child: TextField(
              cursorColor: Colors.blueGrey,
              decoration: InputDecoration(
                hintText: '手机/帐号',
                labelText: '帐号',
                labelStyle: TextStyle(color: Colors.blueGrey),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.blueGrey),
                ),
              ),
              onChanged: (value) {
                this.username = value;
              },
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(
              vertical: 8,
              horizontal: 30,
            ),
            child: TextField(
              cursorColor: Colors.blueGrey,
              keyboardType: TextInputType.visiblePassword,
              obscureText: true,
              decoration: InputDecoration(
                hintText: '请输入您的帐号密码',
                labelText: '密码',
                labelStyle: TextStyle(color: Colors.blueGrey),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.blueGrey),
                ),
              ),
              onChanged: (value) {
                this.psd1 = value;
              },
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(
              vertical: 8,
              horizontal: 30,
            ),
            child: TextField(
              cursorColor: Colors.blueGrey,
              keyboardType: TextInputType.visiblePassword,
              obscureText: true,
              decoration: InputDecoration(
                hintText: '请再次输入您的帐号密码',
                labelText: '确认密码',
                labelStyle: TextStyle(color: Colors.blueGrey),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.blueGrey),
                ),
              ),
              onChanged: (value) {
                this.psd2 = value;
              },
            ),
          ),
          Container(
            width: double.infinity,
            margin: EdgeInsets.symmetric(
              vertical: 8,
              horizontal: 30,
            ),
            child: ElevatedButton(
              child: Container(
                height: 50,
                child: Center(
                  child: Text(
                    '注册',
                    style: TextStyle(color: Colors.white, fontSize: 16),
                  ),
                ),
              ),
              style: ButtonStyle(
                  shape: MaterialStateProperty.all(
                    RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  backgroundColor: MaterialStateProperty.all(Colors.blueGrey)),
              onPressed: () {
                if (this.username != "" && this.psd1 != "" && this.psd2 != "") {
                  if (this.psd1 == this.psd2) {
                    var that = this;
                    Api.sign({"username": that.username, "password": that.psd1})
                        .then((res) {
                      if (res['code'] == 1000) {
                        MyTost.showTost(res['message']);
                        Get.back();
                      } else {
                        MyTost.showTost(res['message']);
                      }
                    });
                  } else {
                    MyTost.showTost('两次密码不一致');
                  }
                } else {
                  MyTost.showTost('输入参数不完整');
                }
              },
            ),
          ),
          Expanded(flex: 1, child: Container()),
        ],
      ),
    ));
  }
}
